# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  include ActionView::Helpers::TextHelper
  include ActionView::Helpers::NumberHelper
  include ActionView::Helpers::FormTagHelper
  include ActionView::Helpers::AssetTagHelper
  include ActionView::Helpers::TagHelper

  def drags_list
  end

  def group
  end

  #icon for user
  def friend_icon(friend, type = 'display', options = {})
    if friend && friend.face
      image_tag friend.face.public_filename(type.to_sym)
    else
      icon("buddy_#{type}.png")
    end
  end


#  def friend_icon(friend, typ = 'display')
#    if app_config[:gravatar] == true
#      site_url = app_config[:url].chomp('/')
#      default = html_escape "#{site_url}#{app_config[:default_icon]}"
#      url = "http://www.gravatar.com/avatar.php?gravatar_id=#{Digest::MD5.hexdigest(friend.email)}&size=#{app_config[:icon_size]}&default=#{default}"
#      image_tag(url, :align => 'absmiddle', :width => app_config['icon_size'], :border => 0, :alt => 'icon for ' + friend.login)
#    elsif !friend.image.blank?
      #image_tag(auth_url(:action => 'image', :id => user.login + '.png'), :align => 'absmiddle', :border => 0, :alt => 'icon for ' + user.login)
#      @friend = friend
#      icon url_for_file_column('friend', 'image', typ)
#    else
#      icon('buddy.png')
#    end
#  end

  #any icon
  def icon(name, imgid=nil)
    options = {:class=> 'icon', :align => 'absmiddle', :border => 0, :alt => name}
    options.update(:id=>imgid) if imgid
    image_tag(name, options)
  end

  #makes a linked image, that lits up, if the first param is true
  def diode(lightup, image, title, options = {}, html_options = nil)
    img = lightup ? 'new'+image : image
    link_to( image_tag(img,:title=>title), options, html_options)
  end

  #makes a remote linked image, that lits up, if the first param is true
  def remote_diode(lightup, image, title, options = {}, html_options = nil)
    img = lightup ? 'new'+image : image
    link_to_remote( image_tag(img,:title=>title), options, html_options)
  end

  #droppable icon for friend
  def friend_drop(friend, type = :display)
    options = {:id=>friend.login, :class=> 'friendicon', :align => 'absmiddle', :border => 0, :alt => friend.login}
    url = friend.face.nil? ? 'buddy.png' : friend.face.public_filename(type)
    "<div id='friendrop#{friend.id}' class='familydrop'>#{image_tag(url, options)}</div>#{ draggable_element('friendrop'+friend.id.to_s, :revert => true) }"
  end

  #draggable icon
  def drag_icon(drag, type = :display)
    o = {:id=>drag.drag_id, :class => 'drag_icon', :align => 'absmiddle', :border => '0', :alt => drag.drag_title}
    <<-HTML
      <div id="#{drag.drag_id}" class="drop">
        #{image_tag(drag.drag_icon(type), o)}
      </div>
      #{ draggable_element(drag.drag_id, :revert => true)}
    HTML
  end

#  def dropicon(name, imgid=nil, friendid=nil)
#    imgoptions = {:class=> 'friendicon', :align => 'absmiddle', :border => 0, :alt => name}
#    imgoptions.update(:id=>imgid) if imgid
#    "<div id='friendrop#{friendid}' class='familydrop'>#{image_tag(name, imgoptions)}</div>#{ draggable_element('friendrop'+friendid.to_s, :revert => true) if friendid }"    
#  end


  # Loading spinner indicator icon tag
  def spinner_tag(id = 'ident')
    image_tag('spinner.gif', :id=>"#{id}_spinner", :align => 'absmiddle', :border=> 0, :style=> 'display: none;', :alt => 'loading...' )
  end
  
  # makes an overbutton image
  def overbutton(image, options = {})
    opts = {:onmouseout => "this.src='/images/#{image}'", :onmouseover => "this.src='/images/over#{image}'"}
    image_tag(image, opts.update(options))   
  end

 #image overbutton
 def tog(img, domid = nil, title = nil, imgoptions = {})
   out, over = image_path(img), image_path('over'+img)
   options = {:onmouseout => "this.src='#{out}'", :onmouseover => "this.src='#{over}'"}
   options.update(:title => "#{title}", :alt => "#{title}")   
   options.update(:onclick => "['#{domid}'].each(Element.toggle)") if domid
   options.update(imgoptions)
   image_tag( img, options )
 end

 def submit_button(img, title = nil)
   out, over = image_path(img), image_path('over'+img)
   options = {:onmouseout => "this.src='#{out}'", :onmouseover => "this.src='#{over}'"}
   options.update(:title => "#{title}", :alt => "#{title}") if title
   tag :input, { "type" => "image", "src" => out }.update(options.stringify_keys)
 end


 def submit_form_button(img, form, title = nil)
   out, over = image_path(img), image_path('over'+img)
   options = {:onmouseout => "this.src='#{out}'", :onmouseover => "this.src='#{over}'"}
   options.update(:title => "#{title}", :alt => "#{title}") if title
   "<a href='#' onclick='$(\'#{form}\').submit();'>" + image_tag( img, options ) + "</a>"
 end

 def button(img, title, linkoptions, htmloptions=nil)
   out, over = image_path(img), image_path('over'+img)
   options = {:style=>"vertical-align:middle;", :onmouseout => "this.src='#{out}'", :onmouseover => "this.src='#{over}'"}
   options.update(:title => "#{title}", :alt => "#{title}") if title
   link_to image_tag( img, options ), linkoptions, htmloptions
 end
 
 #izpljune slikice v draggable divih
 def images_for_visions(visions, type = :small)
  return "" unless visions 
  visions.collect do |v| 
    @vision = v
    path = @vision.public_filename type
    #call = remote_function(:update => "page_content", :url => {:action => "dropvision",:id => @vision.id.to_s})  
    <<-HTML 
    <div id='#{@vision.id.to_s}_vision' class='drops'> 
    #{ image_tag( path ) }
    </div>
    #{ draggable_element(@vision.id.to_s + "_vision", :revert => true) }   
    HTML
  end.join  
 end

 #eternal form buttons X y
 def form_buttons( cancel_linkoptions = {} )
   cancel_img, cancel_title = '_cancel.png', _("Not save")
   ok_img, ok_title = '_ok.png', _("Save")

   cancel_out, cancel_over = image_path(cancel_img), image_path('over'+cancel_img)
   ok_out,     ok_over     = image_path(ok_img), image_path('over'+ok_img)

   ['cancel', 'ok'].each do |o|
     eval( %Q- @#{o}_options = { :class => 'btn_#{o}', :title => #{o}_title, :alt => #{o}_title, :onmouseout => \"this.src='#\{#{o}_out\}'\", :onmouseover => \"this.src='#\{#{o}_over\}'\" }- )
   end

   <<-HTML
     <div class="btns">
     #{link_to( image_tag( cancel_out, @cancel_options ), cancel_linkoptions )}
     #{(tag :input, { "type" => "image", "src" => ok_out }.update(@ok_options.stringify_keys))}
     </div>
   HTML
 end


end
